package com.ruoyi.controller.traceability;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.domain.traceability.MesTraceabilityRecord;
import com.ruoyi.service.traceability.IMesTraceabilityRecordService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 追溯记录Controller
 * 
 * @author ruoyi
 * @date 2024-01-01
 */
@RestController
@RequestMapping("/mes/traceability/record")
public class MesTraceabilityRecordController extends BaseController {
    @Autowired
    private IMesTraceabilityRecordService mesTraceabilityRecordService;

    /**
     * 查询追溯记录列表
     */
    @PreAuthorize("@ss.hasPermi('mes:traceability:list')")
    @GetMapping("/list")
    public TableDataInfo list(MesTraceabilityRecord mesTraceabilityRecord) {
        startPage();
        List<MesTraceabilityRecord> list = mesTraceabilityRecordService.selectMesTraceabilityRecordList(mesTraceabilityRecord);
        return getDataTable(list);
    }

    /**
     * 导出追溯记录列表
     */
    @PreAuthorize("@ss.hasPermi('mes:traceability:export')")
    @Log(title = "追溯记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MesTraceabilityRecord mesTraceabilityRecord) {
        List<MesTraceabilityRecord> list = mesTraceabilityRecordService.selectMesTraceabilityRecordList(mesTraceabilityRecord);
        ExcelUtil<MesTraceabilityRecord> util = new ExcelUtil<MesTraceabilityRecord>(MesTraceabilityRecord.class);
        util.exportExcel(response, list, "追溯记录数据");
    }

    /**
     * 获取追溯记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('mes:traceability:query')")
    @GetMapping(value = "/{traceId}")
    public AjaxResult getInfo(@PathVariable("traceId") Long traceId) {
        return success(mesTraceabilityRecordService.selectMesTraceabilityRecordByTraceId(traceId));
    }

    /**
     * 新增追溯记录
     */
    @PreAuthorize("@ss.hasPermi('mes:traceability:add')")
    @Log(title = "追溯记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MesTraceabilityRecord mesTraceabilityRecord) {
        return toAjax(mesTraceabilityRecordService.insertMesTraceabilityRecord(mesTraceabilityRecord));
    }

    /**
     * 修改追溯记录
     */
    @PreAuthorize("@ss.hasPermi('mes:traceability:edit')")
    @Log(title = "追溯记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MesTraceabilityRecord mesTraceabilityRecord) {
        return toAjax(mesTraceabilityRecordService.updateMesTraceabilityRecord(mesTraceabilityRecord));
    }

    /**
     * 删除追溯记录
     */
    @PreAuthorize("@ss.hasPermi('mes:traceability:remove')")
    @Log(title = "追溯记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{traceIds}")
    public AjaxResult remove(@PathVariable Long[] traceIds) {
        return toAjax(mesTraceabilityRecordService.deleteMesTraceabilityRecordByTraceIds(traceIds));
    }

    /**
     * 根据工单ID查询追溯记录
     */
    @PreAuthorize("@ss.hasPermi('mes:traceability:query')")
    @GetMapping("/workOrder/{workOrderId}")
    public AjaxResult getByWorkOrderId(@PathVariable Long workOrderId) {
        List<MesTraceabilityRecord> list = mesTraceabilityRecordService.selectTraceabilityByWorkOrderId(workOrderId);
        return success(list);
    }

    /**
     * 根据批次号查询追溯记录
     */
    @PreAuthorize("@ss.hasPermi('mes:traceability:query')")
    @GetMapping("/batch/{batchCode}")
    public AjaxResult getByBatchCode(@PathVariable String batchCode) {
        List<MesTraceabilityRecord> list = mesTraceabilityRecordService.selectTraceabilityByBatchCode(batchCode);
        return success(list);
    }

    /**
     * 根据产品ID查询追溯记录
     */
    @PreAuthorize("@ss.hasPermi('mes:traceability:query')")
    @GetMapping("/product/{productId}")
    public AjaxResult getByProductId(@PathVariable Long productId) {
        List<MesTraceabilityRecord> list = mesTraceabilityRecordService.selectTraceabilityByProductId(productId);
        return success(list);
    }

    /**
     * 根据追溯类型查询追溯记录
     */
    @PreAuthorize("@ss.hasPermi('mes:traceability:query')")
    @GetMapping("/type/{traceType}")
    public AjaxResult getByType(@PathVariable String traceType) {
        List<MesTraceabilityRecord> list = mesTraceabilityRecordService.selectTraceabilityByType(traceType);
        return success(list);
    }

    /**
     * 校验追溯编码是否唯一
     */
    @PostMapping("/checkTraceCodeUnique")
    public AjaxResult checkTraceCodeUnique(@RequestBody MesTraceabilityRecord mesTraceabilityRecord) {
        String result = mesTraceabilityRecordService.checkTraceCodeUnique(mesTraceabilityRecord.getTraceCode());
        if ("0".equals(result)) {
            return success();
        }
        return error("追溯编码已存在");
    }

    /**
     * 批量插入追溯记录
     */
    @PreAuthorize("@ss.hasPermi('mes:traceability:add')")
    @Log(title = "追溯记录", businessType = BusinessType.INSERT)
    @PostMapping("/batch")
    public AjaxResult batchInsert(@RequestBody List<MesTraceabilityRecord> traceabilityRecords) {
        return toAjax(mesTraceabilityRecordService.batchInsertTraceabilityRecord(traceabilityRecords));
    }
}